﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using PPI.Client.Interface;
using PPI.Client.Logic;
using PPI.Client.Logic.Delegate;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using PPI.Entities.Settings;

namespace PPI.Client.Presenter
{
    public class DeployInspectionDatabasePresenter : DeployDatabasePresenterBase
    {
        public DeployInspectionDatabasePresenter(IDeployDatabase view)
        :base(view)
        {
        }

        protected override void SetApplicationFiles()
        {
            m_LocalFiles.Push(Constants.K_LOCALAPPLICATIONINSPECTIONMOBILE);
            m_WebFiles.Push(Constants.K_WEBAPPLICATIONINSPECTIONMOBILE);
        }

        protected override void CheckFilesAreUpdate()
        {
            bool filesToDelete = false;
            try
            {
                filesToDelete = LSettings.CheckLocalSettingsWithServer(new ESettingsDeploymentMobileInspection());
            }
            catch (Exception e)
            {
                filesToDelete = true;
            }
            if (filesToDelete)
            {
                if (File.Exists(Constants.K_LOCALAPPLICATIONINSPECTIONMOBILE))
                    File.Delete(Constants.K_LOCALAPPLICATIONINSPECTIONMOBILE);

            }
        }

        public string DeployInspection(ECompany company, EInspector inspector, EHandheld handheld, EVendor_Project project, EVendor_Contact contact, EVendor vendor)
        {
            try
            {
                m_view.IsDownloading = true;
                m_SettingsBase = new ESettingsVersionMobileInspection();
                m_SettingsBase = LSettings.GetSetting(m_SettingsBase);
                string sourceDatabase = LDeploy.StartDeployInspection(handheld.Id, handheld.Name, inspector.Id, inspector.Name, company.Id, company.Name, m_SettingsBase.SettingText,vendor.Id,vendor.Name,project.Id,project.Name,contact.Id,contact.Name);
                m_view.IsDownloading = false;
                return LDeploy.ParseDownloadDatabase(sourceDatabase);
            }
            catch (Exception ex)
            {
                throw new Exception("Not enougth space in disk to copy the database, please delete old images in ftp");
            }
        }
    }
}
